﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using blackhouse.JavaScript;
using blackhouse.JavaScript.JQuery;
using blackhouse.Web.FasterFiles;
using System.Globalization;

namespace blackhouse.Web.Controls {

    /// <summary>
    /// Kontrolka obsługuje listę zdjęć zapisaną w postaci:
    /// <div class="gallery" style="display:none">
    ///     <div>
    ///        <img src="http://bh-dev.aparts.com.pl/ImageFile/FrontBaner/960x720/aparts_2.jpg" class="active" />
    ///         <img src="http://bh-dev.aparts.com.pl/ImageFile/GrayFrontBaner/960x720/aparts_2.jpg" class="gray" />
    ///    </div>
    /// </div>
    /// Czyli mamy holder .gallery. Holder zawiera kolekcję div'ów, gdzie każdy div zawiera: jeden obrazek .active i jeden obrazek gray.
    /// W panelu oznaczonym klasą arrows należy umieścić obrazki z strzałkami. Obrazki powinny być oznaczone klasami: left i right. Dodatkowo można dodać obrazki left-gray i right-gray dla nieaktywnych strzałek (status blur);
    /// </summary>
    public class RotateGallery : Control {

        #region CONST

        private const int ANIMATION_SPEED = 2000;
        private const EasingType ANIMATION_EASING = EasingType.EaseInOutBack;
        private const int INITIAL_FEED_SPEED = 500;
        private const float UNACTIVE_IMAGE_SCALE = 0.915f;
        private const int MARGIN = 30;
        private const int ARROW_MARGIN = 40;
        private const int IMAGE_WIDTH = 960;
        private const int IMAGE_HEIGHT = 720;
        private const string ROTATE_GALLERY_HOLDER = ".rotate-gallery";

        #endregion

        #region Properties

        /// <summary>
        /// Szybkość animacji przełączania zdjęć
        /// </summary>
        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue(RotateGallery.ANIMATION_SPEED)]
        public int AnimationSpeed { get; set; }

        /// <summary>
        /// Szybkość pojawiania się zdjęć podczas ładowania galerii
        /// </summary>
        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue(RotateGallery.INITIAL_FEED_SPEED)]
        public int InitialFeedSpeed { get; set; }

        /// <summary>
        /// Typ animacji 
        /// </summary>
        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue(RotateGallery.ANIMATION_EASING)]
        public EasingType AnimationEasing { get; set; }

        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue(RotateGallery.UNACTIVE_IMAGE_SCALE)]
        public float UnactiveImageScale { get; set; }

        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue(RotateGallery.MARGIN)]
        public int Margin { get; set; }

        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue(RotateGallery.ARROW_MARGIN)]
        public int ArrowMargin { get; set; }

        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue(RotateGallery.IMAGE_WIDTH)]
        public int ImageWidth { get; set; }

        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue(RotateGallery.IMAGE_HEIGHT)]
        public int ImageHeight { get; set; }

        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue(RotateGallery.ROTATE_GALLERY_HOLDER)]
        public string GalleryHolder { get; set; }

        [Browsable(true)]
        [Category("Gallery")]
        [DefaultValue("0.00:00:04")]
        public TimeSpan AutomaticRotateTimeInterval { get; set; }

        #endregion

        #region Constructors

        public RotateGallery() {
            this.AnimationSpeed = ANIMATION_SPEED;
            this.AnimationEasing = ANIMATION_EASING;
            this.InitialFeedSpeed = INITIAL_FEED_SPEED;
            this.UnactiveImageScale = UNACTIVE_IMAGE_SCALE;
            this.Margin = MARGIN;
            this.ArrowMargin = ARROW_MARGIN;
            this.ImageWidth = IMAGE_WIDTH;
            this.ImageHeight = IMAGE_HEIGHT;
            this.GalleryHolder = ROTATE_GALLERY_HOLDER;
            this.AutomaticRotateTimeInterval = TimeSpan.FromMilliseconds(4000);
        }

        #endregion

        #region Event Handlers

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.AddJavaScript();
        }

        #endregion

        #region Methods

        private void AddJavaScript() {
            JQueryLibraries jql = new JQueryLibraries();
            jql.IncludeJQueryEasing();
            jql.IncludeFileBasedOnJQuery("blackhouse.Web.Controls.rotateGallery.js,blackhouse.Web.Controls");

            FasterFilesProvider ffp = new FasterFilesProvider();
            ffp.AddAfterScript(string.Format(CultureInfo.InvariantCulture, @"FG_HOLDER_SELECTOR = '{0}';
FG_ANIM.FadeSpeed={1};
FG_ANIM.AnimSpeed={2};
FG_ANIM.AnimEasing='{3}';
FG_SIZE.Scale={4};
FG_SIZE.Margin={5};
FG_SIZE.Width={6};
FG_SIZE.Height={7};
FG_AUTO_TIME={8};
FG_ARROWS_MARGIN={9};", this.GalleryHolder, this.InitialFeedSpeed, this.AnimationSpeed, this.AnimationEasing.ConvertJsReadable(), this.UnactiveImageScale, this.Margin, this.ImageWidth, this.ImageHeight, Convert.ToInt32(this.AutomaticRotateTimeInterval.TotalMilliseconds), this.ArrowMargin));

        }

        #endregion

    }
}